package com.jy.appmarket.screenlock.adapter;

import com.jy.appmarket.R;
import com.jy.appmarket.adapter.SimpleBaseAdapter;
import com.jy.appmarket.screenlock.model.AppModel;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

/**
 * 类说明：
 * 
 * @author jie.li
 * @date 2015-2-12
 * @version 1.0
 */
public class ShortcutAppMainLockAdapter extends SimpleBaseAdapter<AppModel> {

	private OnShortcutListener mListener;
	private boolean isFlashlightOn = false;

	public interface OnShortcutListener {
		void OnShortcutItemClick(AppModel model);
	}

	public ShortcutAppMainLockAdapter(Context context) {
		super(context);
	}

	@Override
	public int getCount() {
		return super.getCount();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = getInflater().inflate(R.layout.shortcut_app_main_item, parent, false);
			holder.appIcon = convertView.findViewById(R.id.shortcut_app_icon);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final AppModel model = mList.get(position);

		if ("appmarket.screenlock.camera".equals(model.getPkgName())) {
			holder.appIcon.setImageResource(R.drawable.ic_camera);
		} else if ("appmarket.screenlock.flashlight".equals(model.getPkgName())) {
			holder.appIcon.setImageResource(R.drawable.ic_flash_light);
		} else if ("appmarket.screenlock.calculator".equals(model.getPkgName())) {
			holder.appIcon.setImageResource(R.drawable.ic_calculator);
		} else if ("com.jy.appmarket".equals(model.getPkgName())) {
			holder.appIcon.setImageResource(R.drawable.ic_appmarket);
		}

		final int pos = position;
		convertView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mListener != null) {
					if ("appmarket.screenlock.flashlight".equals(model.getPkgName())) {
						isFlashlightOn = !isFlashlightOn;
						if (isFlashlightOn) {
							holder.appIcon.setImageResource(R.drawable.ic_flash_light_on);
						} else {
							holder.appIcon.setImageResource(R.drawable.ic_flash_light);
						}
					}
					mListener.OnShortcutItemClick(mList.get(pos));
				}
			}
		});
		return convertView;
	}

	public void setShortcutListener(OnShortcutListener listener) {
		mListener = listener;
	}

	private static final class ViewHolder {
		ImageView appIcon;
	}
}
